package com.ms.pda.activity

import android.text.Editable
import android.text.TextWatcher
import androidx.databinding.ObservableField
import androidx.lifecycle.ViewModel
import com.blankj.utilcode.util.SPUtils
import com.ms.architecture.base.DataBindingConfig
import com.ms.pda.BR
import com.ms.pda.R
import com.ms.pda.base.BaseActivity
import com.ms.pda.model.Constants
import com.ms.pda.utils.StringUtils


class RFIDSettingActivity : BaseActivity() {

    private var mState: RFIDSettingViewModel? = null

    override fun initViewModel() {
        mState = getActivityScopeViewModel(RFIDSettingViewModel::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig? {
        return DataBindingConfig(R.layout.activity_rfid_setting, BR.state, mState!!)
            .addBindingParam(BR.listener, RFIDSettingListener())
    }

    override fun initViews() {
        super.initViews()
        mState!!.signalIntensity.set(
            SPUtils.getInstance()
                .getInt(Constants.SIGNAL_INTENSITY_KEY, Constants.SIGNAL_INTENSITY_DEFAULT)
                .toString()
        )
        mState!!.frequency.set(
            SPUtils.getInstance()
                .getInt(Constants.FREQUENCY_KEY, Constants.FREQUENCY_DEFAULT).toString()
        )
    }


    open inner class RFIDSettingListener {

    }

    class RFIDSettingViewModel : ViewModel() {
        val signalIntensity: ObservableField<String> = ObservableField()
        val frequency: ObservableField<String> = ObservableField()

        val signalIntensityTextWatcher: TextWatcher = object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                var value = StringUtils.string2Int(s?.toString())
                if (value <= 0) {
                    value = Constants.SIGNAL_INTENSITY_DEFAULT
                }
                SPUtils.getInstance()
                    .put(Constants.SIGNAL_INTENSITY_KEY, value)
            }

        }

        val frequencyTextWatcher: TextWatcher = object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                var value = StringUtils.string2Int(s?.toString())
                if (value <= 0) {
                    value = Constants.FREQUENCY_DEFAULT
                }
                SPUtils.getInstance()
                    .put(Constants.FREQUENCY_KEY, value)
            }

        }
    }
}